//
//  ViewController.m
//  MyNotes
//
//  Created by chen on 17/3/15.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "ViewController.h"
#define CellIdentifier @"CellIdentifier"

@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *datalist;
@property (strong,nonatomic) Note *note;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *editButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _editButton.title = @"Edit";
    _editButton.tag = 0;
    NoteBL *bl = [NoteBL new];
    _datalist = [bl findAll:self.tid];
    self.navigationItem.title = self.title;
    //广播通信
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadtableView:) name:@"GetInformation" object:nil];
}



-(void)reloadtableView:(NSNotification *)notification{
    NSDictionary *dict = [notification userInfo];
    Note *note = [[Note alloc]initWithContent:dict[@"content"] andTid:[dict[@"tid"] integerValue]];
    NoteBL *bl = [NoteBL new];
    [bl createNote:note];
    [_datalist addObject:note];
    [self.tableView reloadData];
    
}
- (IBAction)editCell:(id)sender {
    if (_editButton.tag == 0) {
        [self.tableView setEditing:true animated:true];
        _editButton.title = @"Done";
        _editButton.tag = 1;
        [_editButton titleTextAttributesForState:UIControlStateNormal];
    }else{
        [self.tableView setEditing:false animated:true];
        _editButton.tag = 0;
        _editButton.title = @"Edit";
        [_editButton titleTextAttributesForState:UIControlStateNormal];
    }
}
- (IBAction)addCell:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    AddViewController *addView = [storyboard instantiateViewControllerWithIdentifier:@"AddViewController"] ;
    addView.tid = self.tid;
    [self.navigationController pushViewController:addView animated:TRUE];
}
#pragma mark -- UITableViewDelegate方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _datalist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    _note = _datalist[indexPath.row];
    cell.textLabel.text = _note.content;
    cell.detailTextLabel.text = _note.dateString;
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
     NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NoteBL *bl = [NoteBL new];
        //这里是需要进行判断才行
        [bl remove:_datalist[indexPath.row]];
        [_datalist removeObjectAtIndex: indexPath.row];
        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
    [self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:@"DetailShow" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"DetailShow"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSInteger selectedIndex = [indexPath row];
        DetailViewController *detailViewController = segue.destinationViewController;
        Note *note = _datalist[selectedIndex];
        detailViewController.detail = note.content;
        detailViewController.date = note.dateString;
    }
}

@end
